package com.lingkang.xcoa.controller;

import com.lingkang.xcoa.data.dto.ResponsePageResult;
import com.lingkang.xcoa.data.dto.ResponseResult;
import com.lingkang.xcoa.data.entity.OaUser;
import com.lingkang.xcoa.data.vo.RoleUserListVo;
import com.lingkang.xcoa.service.AdminService;
import com.lingkang.xcoa.service.UserService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author linke
 * @date 2020/8/19 23:15
 * @description
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private AdminService adminService;
  @Autowired
  private UserService userService;

  @GetMapping("index")
  public String index() {
    return "admin/index";
  }

  @GetMapping("welcome")
  public String welcome() {
    return "admin/welcome";
  }

  @GetMapping("peopleAdmin")
  public String peopleAdmin() {
    return "admin/peopleAdmin";
  }

  @GetMapping("getPeopleList")
  @ResponseBody
  public ResponsePageResult getPeopleList(String username, String nameLike, Integer page,
      Integer size) {
    return adminService.getPeopleList(username, nameLike, page, size);
  }

  @GetMapping("processApprove")
  public String processApprove() {
    return "admin/processApprove";
  }

  @GetMapping("iframeEdit")
  public String iframeEdit(String username) {
    OaUser user = userService.getUserInfo(username);
    request.setAttribute("user", user);
    List<RoleUserListVo> listVo = adminService.getRoleList(username);
    request.setAttribute("roles", listVo);
    return "admin/iframeEdit";
  }

  @PostMapping("updateInfo")
  @ResponseBody
  public ResponseResult updateInfo(@RequestParam("role[]") List<Integer> roles, Integer status,
      String username) {
    if(StringUtils.isEmpty(username)){
      return new ResponseResult(1,"username不能为空！");
    }
    return adminService.updateInfo(username, roles, status);
  }
}
